var prefix = ctx + "md/business"
$().ready(function() {
	validateRule();
	initTypeClick();
	getByDetFeildId();
	initLoadFieldId();
	loadTableId();
	initLoadLinkWay();
	initLoadText();
	initLoadAlgorithmType();
	initLoadTypeNode();
	loadLinkWay();
	loadDataSourceType();
	loadBusiUse();
	loadAlgorithmType();
	loadOpenLevel();
	initLoadType();
});

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
function update() {
	$('#dataUseWarning').hide();	
	var dataType = $('#dataType').val();
	var algorithm = $('#algorithmType').val();
	var linkWay = $('#linkWay').val();
	var dataUse = $("input:checkbox[name='dataUse']:checked").map(function(index,elem) {
        return $(elem).val();
    }).get().join(',');
	var ok = true;
	if(algorithm == 1){
		var tableId = $('#tableId').val();
		getTableType(tableId);
	} else if(algorithm == 2) {
		var tableId = $('#stTableId').val();
		getTableType(tableId);
	} else if(algorithm == 5) {
		var tableId = $('#sTableId').val();
		getTableType(tableId);
	}
	
	//如果表不匹配
	if(dataType == 0){//法人
		if(linkWay == 1){//法人和法人
			ok = false;
			parent.layer.alert("数据来源和连接方式不匹配");
		} else if(linkWay == 2 && tableType != 4){//法人和自然人
			ok = false;
			parent.layer.alert("数据来源和连接方式不匹配");
		}
	}else if(dataType == 1){//自然人
		if(linkWay == 1 && tableType != 4){//自然人和法人
			ok = false;
			parent.layer.alert("数据来源和连接方式不匹配");
		} else if(linkWay == 2 &&  tableType != 5){//自然人和自然人
			ok = false;
			parent.layer.alert("数据来源和连接方式不匹配");
		}
	}
	//如果用途为空...
	if(dataUse == null || dataUse == ""){
		//$('#dataUseWarning').show();
		parent.layer.alert("请至少选择一种用途");
		ok = false;
	}
	if(ok){
	if(algorithm == '1'){
		$.ajax({
			cache : true,
			type : "POST",
			url : prefix + "/doEdit",
			dataType: "json",
			data : {
					busiId:$('#id').val(),
					busiName:$('#busiName').val(),
					oldBusiName:$('#oldBusiName').val(),
					dataType:$('#dataType').val(),
					busiType:$('#busiType').val(),
					busiTypeName:$("#busiType option:checked").text(),
					busiTypeNode:$('#busiTypeNode').val(),
					busiTypeNodeName:$("#busiTypeNode option:checked").text(),
					openLevel:$('#openLevel').val(),
					algorithmType:$('#algorithmType').val(),
					linkWay:$('#linkWay').val(),
					dataUse:dataUse,
					tableId:$('#tableId').val(),
					columnId:$('#fieldId').val(),
					busiDesc:$('#busiDesc').val()
					},
			async : false,
			error : function(request) {
				parent.layer.alert("网络超时");
			},
			success : function(data) {
				if (data.code == 0) {
					parent.layer.msg("操作成功");
					parent.reLoad();
					var index = parent.layer.getFrameIndex(window.name);
					parent.layer.close(index);

				} else {
					parent.layer.alert(data.msg)
				}

			}
		});
	}else if(algorithm == '2'){
		if(linkWay == 0 || linkWay == 1 || linkWay == 2){
			$.ajax({
				cache : true,
				type : "POST",
				url : prefix + "/doEdit",
				dataType: "json",
				data : {
						busiId:$('#id').val(),
						busiName:$('#busiName').val(),
						oldBusiName:$('#oldBusiName').val(),
						dataType:$('#dataType').val(),
						busiType:$('#busiType').val(),
						busiTypeName:$("#busiType option:checked").text(),
						busiTypeNode:$('#busiTypeNode').val(),
						busiTypeNodeName:$("#busiTypeNode option:checked").text(),
						openLevel:$('#openLevel').val(),
						algorithmType:$('#algorithmType').val(),
						linkWay:$('#linkWay').val(),
						dataUse:dataUse,
						tableId:$('#stTableId').val(),
						detColumnId:$('#stDetFieldId').val(),
						busiDesc:$('#stBusiDesc').val()
						},
				async : false,
				error : function(request) {
					parent.layer.alert("网络超时");
				},
				success : function(data) {
					if (data.code == 0) {
						parent.layer.msg("操作成功");
						parent.reLoad();
						var index = parent.layer.getFrameIndex(window.name);
						parent.layer.close(index);

					} else {
						parent.layer.alert(data.msg)
					}

				}
			});
		}else{
			$.ajax({
				cache : true,
				type : "POST",
				url : prefix + "/doEdit",
				dataType: "json",
				data : {
						busiId:$('#id').val(),
						busiName:$('#busiName').val(),
						oldBusiName:$('#oldBusiName').val(),
						dataType:$('#dataType').val(),
						busiType:$('#busiType').val(),
						busiTypeName:$("#busiType option:checked").text(),
						busiTypeNode:$('#busiTypeNode').val(),
						busiTypeNodeName:$("#busiTypeNode option:checked").text(),
						openLevel:$('#openLevel').val(),
						algorithmType:$('#algorithmType').val(),
						linkWay:$('#linkWay').val(),
						dataUse:dataUse,
						tableId:$('#stTableId').val(),
						busiDesc:$('#stBusiDesc').val()
						},
				async : false,
				error : function(request) {
					parent.layer.alert("网络超时");
				},
				success : function(data) {
					if (data.code == 0) {
						parent.layer.msg("操作成功");
						parent.reLoad();
						var index = parent.layer.getFrameIndex(window.name);
						parent.layer.close(index);

					} else {
						parent.layer.alert(data.msg)
					}

				}
			});
		}
		
	}else if(algorithm == '3'){
		if(linkWay == 0 || linkWay == 1 || linkWay == 2){
			$.ajax({
				cache : true,
				type : "POST",
				url : prefix + "/doEdit",
				dataType: "json",
				data : {
						busiId:$('#id').val(),
						busiName:$('#busiName').val(),
						oldBusiName:$('#oldBusiName').val(),
						dataType:$('#dataType').val(),
						busiType:$('#busiType').val(),
						busiTypeName:$("#busiType option:checked").text(),
						busiTypeNode:$('#busiTypeNode').val(),
						busiTypeNodeName:$("#busiTypeNode option:checked").text(),
						openLevel:$('#openLevel').val(),
						algorithmType:$('#algorithmType').val(),
						linkWay:$('#linkWay').val(),
						dataUse:dataUse,
						dataSource:$('#dataSource').val(),
						busiSql:$('#busiSql').val(),
						busiDesc:$('#cBusiDesc').val(),
						busiSqldet:$('#busiSqldet').val(),
						busiDescdet:$('#busiDescdet').val()
						},
				async : false,
				error : function(request) {
					parent.layer.alert("网络超时");
				},
				success : function(data) {
					if (data.code == 0) {
						parent.layer.msg("操作成功");
						parent.reLoad();
						var index = parent.layer.getFrameIndex(window.name);
						parent.layer.close(index);
					} else {
						parent.layer.alert(data.msg)
					}

				}
			});
		}else{
			$.ajax({
				cache : true,
				type : "POST",
				url : prefix + "/doEdit",
				dataType: "json",
				data : {
						busiId:$('#id').val(),
						busiName:$('#busiName').val(),
						oldBusiName:$('#oldBusiName').val(),
						dataType:$('#dataType').val(),
						busiType:$('#busiType').val(),
						busiTypeName:$("#busiType option:checked").text(),
						busiTypeNode:$('#busiTypeNode').val(),
						busiTypeNodeName:$("#busiTypeNode option:checked").text(),
						openLevel:$('#openLevel').val(),
						algorithmType:$('#algorithmType').val(),
						linkWay:$('#linkWay').val(),
						dataUse:dataUse,
						dataSource:$('#dataSource').val(),
						busiSql:$('#busiSql').val(),
						busiDesc:$('#cBusiDesc').val()
						},
				async : false,
				error : function(request) {
					parent.layer.alert("网络超时");
				},
				success : function(data) {
					if (data.code == 0) {
						parent.layer.msg("操作成功");
						parent.reLoad();
						var index = parent.layer.getFrameIndex(window.name);
						parent.layer.close(index);
					} else {
						parent.layer.alert(data.msg)
					}

				}
			});
		}
	}else if(algorithm == '4'){
		$.ajax({
			cache : true,
			type : "POST",
			url : prefix + "/doEdit",
			dataType: "json",
			data : {
					busiId:$('#id').val(),
					busiName:$('#busiName').val(),
					oldBusiName:$('#oldBusiName').val(),
					dataType:$('#dataType').val(),
					busiType:$('#busiType').val(),
					busiTypeName:$("#busiType option:checked").text(),
					busiTypeNode:$('#busiTypeNode').val(),
					busiTypeNodeName:$("#busiTypeNode option:checked").text(),
					openLevel:$('#openLevel').val(),
					algorithmType:$('#algorithmType').val(),
					linkWay:$('#linkWay').val(),
					dataUse:dataUse,
					springBean:$('#springBean').val(),
					interfaceName:$('#interfaceName').val(),
					busiDesc:$('#iBusiDesc').val(),
					busiDesc:$('#iBusiDesc').val()
					},
			async : false,
			error : function(request) {
				parent.layer.alert("网络超时");
			},
			success : function(data) {
				if (data.code == 0) {
					parent.layer.msg("操作成功");
					parent.reLoad();
					var index = parent.layer.getFrameIndex(window.name);
					parent.layer.close(index);

				} else {
					parent.layer.alert(data.msg)
				}

			}
		});
	}else if(algorithm == '5'){
		if(linkWay == '0' || linkWay == '1' || linkWay == '2'){//链接方式为明细/法人信用记录/自然人信用记录
			$.ajax({
				cache : true,
				type : "POST",
				url : prefix + "/doEdit",
				dataType: "json",
				data : {
						busiId:$('#id').val(),
						busiName:$('#busiName').val(),
						oldBusiName:$('#oldBusiName').val(),
						dataType:$('#dataType').val(),
						busiType:$('#busiType').val(),
						busiTypeName:$("#busiType option:checked").text(),
						busiTypeNode:$('#busiTypeNode').val(),
						busiTypeNodeName:$("#busiTypeNode option:checked").text(),
						openLevel:$('#openLevel').val(),
						algorithmType:$('#algorithmType').val(),
						linkWay:$('#linkWay').val(),
						dataUse:dataUse,
						tableId:$('#sTableId').val(),
						columnId:$('#sFieldId').val(),
						columnValue:$('#fieldValue').val(),
						detColumnId:$('#sDetFieldId').val(),
						busiDesc:$('#sBusiDesc').val()
						},
				async : false,
				error : function(request) {
					parent.layer.alert("网络超时");
				},
				success : function(data) {
					if (data.code == 0) {
						parent.layer.msg("操作成功");
						parent.reLoad();
						var index = parent.layer.getFrameIndex(window.name);
						parent.layer.close(index);

					} else {
						parent.layer.alert(data.msg)
					}

				}
			});
		} else {
			$.ajax({
				cache : true,
				type : "POST",
				url : prefix + "/doEdit",
				dataType: "json",
				data : {
						busiId:$('#id').val(),
						busiName:$('#busiName').val(),
						oldBusiName:$('#oldBusiName').val(),
						dataType:$('#dataType').val(),
						busiType:$('#busiType').val(),
						busiTypeName:$("#busiType option:checked").text(),
						busiTypeNode:$('#busiTypeNode').val(),
						busiTypeNodeName:$("#busiTypeNode option:checked").text(),
						openLevel:$('#openLevel').val(),
						algorithmType:$('#algorithmType').val(),
						linkWay:$('#linkWay').val(),
						dataUse:dataUse,
						tableId:$('#sTableId').val(),
						columnId:$('#sFieldId').val(),
						columnValue:$('#fieldValue').val(),
						busiDesc:$('#sBusiDesc').val()
						},
				async : false,
				error : function(request) {
					parent.layer.alert("网络超时");
				},
				success : function(data) {
					if (data.code == 0) {
						parent.layer.msg("操作成功");
						parent.reLoad();
						var index = parent.layer.getFrameIndex(window.name);
						parent.layer.close(index);

					} else {
						parent.layer.alert(data.msg)
					}

				}
			});
		}
		/*$.ajax({
			cache : true,
			type : "POST",
			url : prefix + "/doEdit",
			dataType: "json",
			data : {
					busiId:$('#id').val(),
					busiName:$('#busiName').val(),
					oldBusiName:$('#oldBusiName').val(),
					dataType:$('#dataType').val(),
					busiType:$('#busiType').val(),
					busiTypeName:$("#busiType option:checked").text(),
					busiTypeNode:$('#busiTypeNode').val(),
					busiTypeNodeName:$("#busiTypeNode option:checked").text(),
					openLevel:$('#openLevel').val(),
					algorithmType:$('#algorithmType').val(),
					linkWay:$('#linkWay').val(),
					dataUse:dataUse,
					tableId:$('#sTableId').val(),
					columnId:$('#sFieldId').val(),
					columnValue:$('#fieldValue').val(),
					busiDesc:$('#sBusiDesc').val()
					},
			async : false,
			error : function(request) {
				parent.layer.alert("网络超时");
			},
			success : function(data) {
				if (data.code == 0) {
					parent.layer.msg("操作成功");
					parent.reLoad();
					var index = parent.layer.getFrameIndex(window.name);
					parent.layer.close(index);

				} else {
					parent.layer.alert(data.msg)
				}

			}
		});*/
	}
	}
}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			busiName : {
				required : true
			},
		},
		messages : {
			busiName : {
				required : icon + "请输入业务元名称"
			},
			
		},
	})
}

function loadOpenLevel(){
	var html = "";
	$.ajax({
		url : ctx + 'md/business/byValue',
		data : {
			'value' : 'open_level'
		},
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				if($('#oopenLevel').val() == data[i].id){
					html += '<option value="' + data[i].id + '" selected="selected">' + data[i].name + '</option>';
				}else{
					html += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
				}
			}
			$('#openLevel').append(html);
		}
	});
}
function initTypeClick() {
	$('#dataType').on('change', function() {
		loadType();
		$("#busiTypeNode").html('<option value="">--请选择业务小类--</option>');
		$("#tableId").html('<option value="">--请选择表--</option>');
		$("#stTableId").html('<option value="">--请选择表--</option>');
		$("#sTableId").html('<option value="">--请选择表--</option>');
		$("#fieldId").html('<option value="">--请选择字段--</option>');
		$("#sFieldId").html('<option value="">--请选择字段--</option>');
		$('#stDetFieldName').val("");
		$('#stDetFieldId').val("");
		$('#sDetFieldName').val("");
		$('#sDetFieldId').val("");
		loadTableId();
	});
}

function loadType(){
	var html = "";
	var dataType = $('#dataType').val();
	var value = "";
	if(dataType == '0'){
		value =	'legal_person';
	}else if(dataType == '1'){
		value =	'natural_person';
	}
	$.ajax({
		url : ctx + 'md/business/byValue',
		data : {
			'value' : value
		},
		success : function(data) {
			$("#busiType").html('<option value="">--请选择业务大类--</option>');
			//加载数据
			for (var i = 0; i < data.length; i++) {
				if($('#busiType').val() == data[i].id){
					html += '<option value="' + data[i].id + '" selected="selected">' + data[i].name + '</option>';
				}else{
					html += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
				}
				
			}
			$('#busiType').append(html);
			//点击事件
			$('#busiType').on('change', function() {
				var a = $('#busiType').val();
				loadTypeNode(a);
			});
		}
	});
}
function initLoadType(){
	var html = "";
	var dataType = $('#dataType').val();
	var value = "";
	if(dataType == '0'){
		value =	'legal_person';
	}else if(dataType == '1'){
		value =	'natural_person';
	}
	$.ajax({
		url : ctx + 'md/business/byValue',
		data : {
			'value' : value
		},
		success : function(data) {
			$("#busiType").html('<option value="">--请选择业务大类--</option>');
			//加载数据
			for (var i = 0; i < data.length; i++) {
				if($('#obusiType').val() == data[i].id){
					html += '<option value="' + data[i].id + '" selected="selected">' + data[i].name + '</option>';
				}else{
					html += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
				}
				
			}
			$('#busiType').append(html);
			//点击事件
			$('#busiType').on('change', function() {
				var a = $('#busiType').val();
				loadTypeNode(a);
			});
		}
	});
}

function initLoadTypeNode(){
	var html = '';
	$.ajax({
		url : ctx + 'md/business/busiTypeNode',
		data : {
			'parentId' : $('#obusiType').val()
		},
		success : function(data) {
			$("#busiTypeNode").html('<option value="">--请选择业务小类--</option>');
			//加载数据
			for (var i = 0; i < data.length; i++) {
				if($('#obusiTypeNode').val() == data[i].id){
					html += '<option value="' + data[i].id + '" selected="selected">' + data[i].name + '</option>';
				}else{
					html += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
				}
			}
			$("#busiTypeNode").append(html);
		}
	});
}
function loadTypeNode(parentId){
	var html = '';
	$.ajax({
		url : ctx + 'md/business/busiTypeNode',
		data : {
			'parentId' : parentId
		},
		success : function(data) {
			$("#busiTypeNode").html('<option value="">--请选择业务小类--</option>');
			//加载数据
			for (var i = 0; i < data.length; i++) {
				if($('#busiTypeNode').val() == data[i].id){
					html += '<option value="' + data[i].id + '" selected="selected">' + data[i].name + '</option>';
				}else{
					html += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
				}
			}
			$("#busiTypeNode").append(html);
		}
	});
}
/**
 * 初始根据算法类型加载页面
 */
function initLoadAlgorithmType(){
	var algorithmType = $('#algorithmType').val();
	if(algorithmType == "1"){
		$('#simple').show();
		$('#simpleTable').hide();
		$('#customize').hide();
		$('#interface').hide();
		$('#splitStatistics').hide();
	}else if(algorithmType == "2"){
		$('#simple').hide();
		$('#simpleTable').show();
		$('#customize').hide();
		$('#interface').hide();
		$('#splitStatistics').hide();
	}else if(algorithmType == "3"){
		$('#simple').hide();
		$('#simpleTable').hide();
		$('#customize').show();
		$('#interface').hide();
		$('#splitStatistics').hide();
	}else if(algorithmType == "4"){
		$('#simple').hide();
		$('#simpleTable').hide();
		$('#customize').hide();
		$('#interface').show();
		$('#splitStatistics').hide();
	}else if(algorithmType == "5"){
		$('#simple').hide();
		$('#simpleTable').hide();
		$('#customize').hide();
		$('#interface').hide();
		$('#splitStatistics').show();
	}
}
/**
 * 根据算法类型加载页面
 */
function loadAlgorithmType(){
	$('#algorithmType').on('change', function() {
		initLoadAlgorithmType();
	});
}
/**
 * 加载用途复选框
 */
function loadBusiUse(){
	var html = "";
	$.ajax({
		url : ctx + 'md/business/byValue',
		data : {
			'value' : 'busi_use'
		},
		success : function(data) {
			var dataUses = $('#odataUse').val().split(",");
			//加载数据
			for (var i = 0; i < data.length; i++) {
				//console.log($.inArray(data[i].id, dataUses));
				if($.inArray(String(data[i].value), dataUses) >= 0){
					html += '<label class="control-label" style="font-weight: normal;"><input type="checkbox" id="dataUse" name="dataUse" value="' + data[i].value + '" checked="checked">' + data[i].name + '&nbsp;&nbsp;&nbsp;&nbsp;</label>';
				}else{
					html += '<label class="control-label" style="font-weight: normal;"><input type="checkbox" id="dataUse" name="dataUse" value="' + data[i].value + '">' + data[i].name + '&nbsp;&nbsp;&nbsp;&nbsp;</label>';
				}
			}
			$('#dataUseDiv').append(html);
			$('#dataUseDiv').append('<span style="display: none;color: #cc5965;" id="dataUseWarning"><i class="fa fa-times-circle"></i>至少选择一项用途</span>');
		}
	});
}
/**
 * 加载数据源下拉框
 */
function loadDataSourceType(){
	var html = "";
	$.ajax({
		url : ctx + 'md/source/getAllSource',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				if($('#odataSource').val() == data[i].sourceId){
					html += '<option value="' + data[i].sourceId + '" selected="selected">' + data[i].sourceName + '</option>';
				}else{
					html += '<option value="' + data[i].sourceId + '">' + data[i].sourceName + '</option>';
				}	
			}
			$('#dataSource').append(html);
		}
	});
}
function loadLinkWay(){
	$('#linkWay').on('change', function() {
		initLoadLinkWay();
	});
}

function initLoadLinkWay(){
	var linkWay = $('#linkWay').val();
	if(linkWay == '0' || linkWay == '1' || linkWay == '2'){
		$('#detailDiv').show();
		$('#detailDiv1').show();
		$('#detailDiv2').show();
	}else{
		$('#detailDiv').hide();
		$('#detailDiv1').hide();
		$('#detailDiv2').hide();
	}
}

function initLoadText(){
	var algorithmType = $('#algorithmType').val();
	var busiDesc = $('#obusiDesc').val();
	var busiSql = $('#obusiSql').val();
	var busiSqldet = $('#obusiSqldet').val();
	var busiDescdet = $('#obusiDescdet').val();
	if(algorithmType == "1"){//单项
		$('#busiDesc').val(busiDesc);
	}else if(algorithmType == "2"){//单表统计
		$('#stBusiDesc').val(busiDesc);
	}else if(algorithmType == "3"){//自定义
		$('#cBusiDesc').val(busiDesc);
		$('#busiSql').val(busiSql);
		$('#busiSqldet').val(busiSqldet);
		$('#busiDescdet').val(busiDescdet);
	}else if(algorithmType == "4"){//接口访问
		$('#iBusiDesc').val(busiDesc);
	}else if(algorithmType == "5"){//拆分统计
		$('#sBusiDesc').val(busiDesc);
	}
}

/**
 * 加载表下拉框
 */
function loadTableId(){
	var html = '';
	$.ajax({
		url : ctx + 'md/business/getTable',
		data : {
			'value' : 'busi_type',
			dataType : $("#dataType").val()
		},
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				if($('#otableId').val() == data[i].tableId){
					html += '<option value="' + data[i].tableId + '" selected="selected">' + data[i].tableName +((data[i].tableComment == null || data[i].tableComment == "")?"":" ("+data[i].tableComment+") ")+  '</option>'	
				}else{
					html += '<option value="' + data[i].tableId + '">' + data[i].tableName +((data[i].tableComment == null || data[i].tableComment == "")?"":" ("+data[i].tableComment+") ")+  '</option>'	
				}
			}
			$("#tableId").append(html);
			$("#stTableId").append(html);
			$("#sTableId").append(html);
			//点击事件			
			$('#tableId').on('change', function() {
				var a = $('#tableId').val();
				loadFieldId(a);
			});
			$('#stTableId').on('change', function() {
				var a = $('#stTableId').val();
				loadFieldId(a);
				$('#stDetFieldName').val("");
				$('#stDetFieldId').val("");
			});
			$('#sTableId').on('change', function() {
				var a = $('#sTableId').val();
				loadFieldId(a);
				$('#sDetFieldName').val("");
				$('#sDetFieldId').val("");
			});
		}
	});
}
/**
 * 加载字段下拉框
 */
function loadFieldId(tableId){
	var html = '';
	$.ajax({
		url : ctx + 'md/business/getField',
		data : {
			'tableId' : tableId
		},
		success : function(data) {
			$("#fieldId").html('<option value="">--请选择字段--</option>');
			$("#sFieldId").html('<option value="">--请选择字段--</option>');
			//加载数据
			for (var i = 0; i < data.length; i++) {
				/*if($('#ofieldId').val() == data[i].id){
					html += '<option value="' + data[i].columnId + '" selected="selected">' + data[i].columnName +((data[i].columnComment == null || data[i].columnComment == "")?"":" ("+data[i].columnComment+") ")+  '</option>'	
				}else{
					html += '<option value="' + data[i].columnId + '">' + data[i].columnName +((data[i].columnComment == null || data[i].columnComment == "")?"":" ("+data[i].columnComment+") ")+  '</option>'	
				}*/
				if($('#ofieldId').val() == data[i].columnId){
					html += '<option selected="selected" value="' + data[i].columnId + '">' + data[i].columnName +((data[i].columnComment == null || data[i].columnComment == "")?"":" ("+data[i].columnComment+") ")+  '</option>'	
				}else{
					html += '<option value="' + data[i].columnId + '">' + data[i].columnName +((data[i].columnComment == null || data[i].columnComment == "")?"":" ("+data[i].columnComment+") ")+  '</option>'	
				}
			}
			$("#fieldId").append(html);
			$("#sFieldId").append(html);
		}
	});
}

function initLoadFieldId(){
	var html = '';
	$.ajax({
		url : ctx + 'md/business/getField',
		data : {
			'tableId' : $('#otableId').val()
		},
		success : function(data) {
			$("#fieldId").html('<option value="">--请选择字段--</option>');
			$("#sFieldId").html('<option value="">--请选择字段--</option>');
			//加载数据
			for (var i = 0; i < data.length; i++) {
				if($('#ofieldId').val() == data[i].columnId){
					html += '<option value="' + data[i].columnId + '" selected="selected">' + data[i].columnName +((data[i].columnComment == null || data[i].columnComment == "")?"":" ("+data[i].columnComment+") ")+  '</option>'	
				}else{
					html += '<option value="' + data[i].columnId + '">' + data[i].columnName +((data[i].columnComment == null || data[i].columnComment == "")?"":" ("+data[i].columnComment+") ")+  '</option>'	
				}
			}
			$("#fieldId").append(html);
			$("#sFieldId").append(html);
		}
	});
}
/**
 * 根据明细字段id来获取字段名称
 */
function getByDetFeildId(){
	var id = $("#odetFieldId").val();
	$.ajax({
		url : ctx + 'md/business/getByDetFieldId',
		data : {
			'id' : id
		},
		success : function(data) {
			//加载数据
			$("#stDetFieldName").val(data);
			$("#sDetFieldName").val(data);
		}
	});
}

function toDetField(){
	var algorithmType = $('#algorithmType').val();
	if(algorithmType == 2){//算法类型为单表统计
		var tableId = $('#stTableId').val();
	}else if(algorithmType == 5){//算法类型为拆分统计
		var tableId = $('#sTableId').val();
	}
	if(tableId != null && tableId != ""){
		var page = layer.open({
			type : 2,
			title : '明细字段',
			maxmin : true,
			shadeClose : false, // 点击遮罩关闭层
			area : [ '800px', '520px' ],
			content : prefix + '/toDetField/' + tableId // iframe的url
		});
	}else{
		parent.layer.alert("请选择数据源");
	}
}

/**
 * 获取所选表类型
 * @param tableId
 */
function getTableType(tableId){
	$.ajax({
		url : ctx + 'md/business/getTableType',
		contentType : 'text',
		async : false,
		data : {
			'tableId' : tableId
		},
		success : function(data) {
			window.tableType = data;
		}
	});
}